<?php
namespace org;

class StrToTime{
	protected static $key;

	protected static $time_str = [];
	//月份
	protected static $month = [
		'下个月' => '+1 month',
		'下月'   => '+1 month',
		'两个月以后' => '+2 month',

		'1月'    => 1,
		'一月'	 => 1,
		'2月'    => 2,
		'二月'	 => 2,
		'3月'	 => 3,
		'三月'	 => 3,
		'4月'	 => 4,
		'四月'	 => 4,
		'5月'	 => 5,
		'五月'	 => 5,
		'6月'	 => 6,
		'六月'	 => 6,
		'7月'	 => 7,
		'七月'	 => 7,
		'8月'	 => 8,
		'八月'	 => 8,
		'9月'	 => 9,
		'九月'	 => 9,
		'10月'	 => 10,
		'十月'	 => 10,
		'11月'	 => 11,
		'十一月'	 => 11,
		'12月'	 => 12,
		'十二月'  => 12,
	];
	//周
	protected static $day = [
		'今天'   =>'+0 day',
		'明天'   =>'+1 day',
		'后天'   =>'+2 day',
		'大后天' =>'+3 day',


		'周一'	 => 1,
		'周二'	 => 2,
		'周三'	 => 3,
		'周四'	 => 4,
		'周五'	 => 5,
		'周六'	 => 6,
		'周末'	 => 6,
		'周日'	 => 7,
		'周天'	 => 7,

		'周1'	 => 1,
		'周2' 	 => 2,
		'周3' 	 => 3,
		'周4'	 => 4,
		'周5'	 => 5,
		'周6' 	 => 6,
		'周7' 	 => 7,

		'星期1'	 => 1,
		'星期2' 	 => 2,
		'星期3' 	 => 3,
		'星期4'	 => 4,
		'星期5'	 => 5,
		'星期6' 	 => 6,
		'星期7' 	 => 7,

		'星期一'	 => 1,
		'星期二'	 => 2,
		'星期三'	 => 3,
		'星期四'	 => 4,
		'星期五'	 => 5,
		'星期六'	 => 6,
		'星期末'	 => 6,
		'星期日'	 => 7,
		'星期天'	 => 7,

		'礼拜1'	 => 1,
		'礼拜2' 	 => 2,
		'礼拜3' 	 => 3,
		'礼拜4'	 => 4,
		'礼拜5'	 => 5,
		'礼拜6' 	 => 6,
		'礼拜7' 	 => 7,

		'礼拜一'	 => 1,
		'礼拜二'	 => 2,
		'礼拜三'	 => 3,
		'礼拜四'	 => 4,
		'礼拜五'	 => 5,
		'礼拜六'	 => 6,
		'礼拜末'	 => 6,
		'礼拜日'	 => 7,

		'下周一'	 => 8,
		'下周二'	 => 9,
		'下周三'	 => 10,
		'下周四'	 => 11,
		'下周五'	 => 12,
		'下周六'	 => 13,
		'下周末'	 => 14,

		'下周1'	 => 8,
		'下周2'	 => 9,
		'下周3'	 => 10,
		'下周4'	 => 11,
		'下周5'	 => 12,
		'下周6'	 => 13,
		'下周7'	 => 14,

		'下星期一'	 => 8,
		'下星期二'	 => 9,
		'下星期三'	 => 10,
		'下星期四'	 => 11,
		'下星期五'	 => 12,
		'下星期六'	 => 13,
		'下星期日'	 => 14,

		'下星期1'	 => 8,
		'下星期2'	 => 9,
		'下星期3'	 => 10,
		'下星期4'	 => 11,
		'下星期5'	 => 12,
		'下星期6'	 => 13,
		'下星期7'	 => 14,

		'下礼拜1'	 => 8,
		'下礼拜2' 	 => 9,
		'下礼拜3' 	 => 10,
		'下礼拜4'	 => 11,
		'下礼拜5'	 => 12,
		'下礼拜6' 	 => 13,
		'下礼拜7' 	 => 14,

		'下礼拜一'	 => 8,
		'下礼拜二'	 => 9,
		'下礼拜三'	 => 10,
		'下礼拜四'	 => 11,
		'下礼拜五'	 => 12,
		'下礼拜六'	 => 13,
		'下礼拜末'	 => 14,
		'下礼拜日'	 => 14
	];
	//日期
	protected static $number = [
		'中旬'  => 11,

		'1号'    => 1,
		'一号'   => 1,
		'一日'   => 1,
		'1日'    => 1,

		'2号'    => 2,
		'二号'   => 2,
		'二日'   => 2,
		'2日'    => 2,

		'3号'    => 3,
		'三号'   => 3,
		'3日'    => 3,
		'三日'   => 3,

		'4号'    => 4,
		'四号'   => 4,
		'4日'    => 4,
		'四日'   => 4,

		'5号'    => 5,
		'五号'   => 5,
		'5日'    => 5,
		'五日'   => 5,

		'6号'    => 6,
		'六号'   => 6,
		'6日'    => 6,
		'六日'   => 6,

		'7号'    => 7,
		'七号'   => 7,
		'7日'    => 7,
		'七日'   => 7,

		'8号'    => 8,
		'八号'   => 8,
		'8日'    => 8,
		'八日'   => 8,

		'9号'    => 9,
		'九号'   => 9,
		'9日'    => 9,
		'九日'   => 9,

		'10号'    => 10,
		'十号'   => 10,
		'10日'    => 10,
		'十日'   => 10,

		'11号'    => 11,
		'十一号'   => 11,
		'11日'    => 11,
		'十一日'   => 11,

		'12号'    => 12,
		'十二号'   => 12,
		'12日'    => 12,
		'十二日'   => 12,

		'13号'    => 13,
		'十三号'   => 13,
		'13日'    => 13,
		'十三日'   => 13,

		'14号'    => 14,
		'十四号'   => 14,
		'14日'    => 14,
		'十四日'   => 14,

		'15号'    => 15,
		'十五号'   => 15,
		'15日'    => 15,
		'十五日'   => 15,

		'16号'    => 16,
		'十六号'   => 16,
		'16日'    => 16,
		'十六日'   => 16,

		'17号'    => 17,
		'十七号'   => 17,
		'17日'    => 17,
		'十七日'   => 17,

		'18号'    => 18,
		'十八号'   => 18,
		'18日'    => 18,
		'十八日'   => 18,

		'19号'    => 19,
		'十九号'   => 19,
		'19日'    => 19,
		'十九日'   => 19,

		'20号'    => 20,
		'二十号'   => 20,
		'20日'    => 20,
		'二十日'   => 20,

		'21号'    => 21,
		'二十一号'   => 21,
		'21日'    => 21,
		'二十一日'   => 21,

		'22号'    => 22,
		'二十二号'   => 22,
		'22日'    => 22,
		'二十二日'   => 22,

		'23号'    => 23,
		'二十三号'   => 23,
		'23日'    => 23,
		'二十三日'   => 23,

		'24号'    => 24,
		'二十四号'   => 24,
		'24日'    => 24,
		'二十四日'   => 24,

		'25号'    => 25,
		'二十五号'   => 25,
		'25日'    => 25,
		'二十五日'   => 25,

		'26号'    => 26,
		'二十六号'   => 26,
		'26日'    => 26,
		'二十六日'   => 26,

		'27号'    => 27,
		'二十七号'   => 27,
		'27日'    => 27,
		'二十七日'   => 27,

		'28号'       => 28,
		'二十八号'    => 28,
		'28日'       => 28,
		'二十八日'    => 28,

		'29号'    => 29,
		'29号'   => 29,
		'29日'    => 29,
		'二十九日'   => 29,

		'30号'    => 30,
		'三十号'   => 30,
		'30日'    => 30,
		'三十日'   => 30,

		'31号'      => 31,
		'三十一号'   => 31,
		'31日'       => 31,
		'三十一日'   => 31,
	];
	//修饰词
	protected static $bedeck = [
		'上午'  => 'upper',
		'下午'  => 'lower',
		'晚上'  => 'lower'
	];
	//小时
	protected static $hour = [
			'0点'		=>0,
			'1点'		=>1,
			'2点'		=>2,
			'3点'		=>3,
			'4点'		=>4,
			'5点'		=>5,
			'6点'		=>6,
			'7点'		=>7,
			'8点'		=>8,
			'9点'		=>9,
			'10点'		=>10,
			'11点'		=>11,
			'12点'		=>12,
			'13点'		=>13,
			'14点'		=>14,
			'15点'		=>15,
			'16点'		=>16,
			'17点'		=>17,
			'18点'		=>18,
			'19点'		=>19,
			'20点'		=>20,
			'21点'		=>21,
			'22点'		=>22,
			'23点'		=>23,

			'零点'		=>0,
			'一点'		=>1,
			'二点'		=>2,
			'两点'	    =>2,
			'三点'		=>3,
			'四点'		=>4,
			'五点'		=>5,
			'六点'		=>6,
			'七点'		=>7,
			'八点'		=>8,
			'九点'		=>9,
			'十点'		=>10,
			'十一点'		=>11,
			'十二点'		=>12,
			'十三点'		=>13,
			'十四点'		=>14,
			'十五点'		=>15,
			'十六点'		=>16,
			'十七点'		=>17,
			'十八点'		=>18,
			'十九点'		=>19,
			'二十点'		=>20,
			'二十一点'	=>21,
			'二十二点'	=>22,
			'二十三点'   =>23,
		];
	//分钟
	protected static $minute = [
		'半'	 		=> 30,
		'一刻'		=> 15,

		'0'		=> 0,
		'1'		=> 1,
		'2'		=> 2,
		'3'		=> 3,
		'4'		=> 4,
		'5'		=> 5,
		'6'		=> 6,
		'7'		=> 7,
		'8'		=> 8,
		'9'		=> 9,
		'10'	=> 10,
		'11'	=> 11,
		'12'	=> 12,
		'13'	=> 13,
		'14'	=> 14,
		'15'	=> 15,
		'16'	=> 16,
		'17'	=> 17,
		'18'	=> 18,
		'19'	=> 19,
		'20'	=> 20,
		'21'	=> 21,
		'22'	=> 22,
		'23'	=> 23,
		'24'	=> 24,
		'25'	=> 25,
		'26'	=> 26,
		'27'	=> 27,
		'28'	=> 28,
		'29'	=> 29,
		'30'	=> 30,
		'31'	=> 31,
		'32'	=> 32,
		'33'	=> 33,
		'34'	=> 34,
		'35'	=> 35,
		'36'	=> 36,
		'37'	=> 37,
		'38'	=> 38,
		'39'	=> 39,
		'40'	=> 40,
		'41'	=> 41,
		'42'	=> 42,
		'43'	=> 43,
		'44'	=> 44,
		'45'	=> 45,
		'46'	=> 46,
		'47'	=> 47,
		'48'	=> 48,
		'49'	=> 49,
		'50'	=> 50,
		'51'	=> 51,
		'52'	=> 52,
		'53'	=> 53,
		'54'	=> 54,
		'55'	=> 55,
		'56'	=> 56,
		'57'	=> 57,
		'58'	=> 58,
		'59'	=> 59,

		'0分'		=> 0,
		'1分'		=> 1,
		'2分'		=> 2,
		'3分'		=> 3,
		'4分'		=> 4,
		'5分'		=> 5,
		'6分'		=> 6,
		'7分'		=> 7,
		'8分'		=> 8,
		'9分'		=> 9,
		'10分'		=> 10,
		'11分'		=> 11,
		'12分'		=> 12,
		'13分'		=> 13,
		'14分'		=> 14,
		'15分'		=> 15,
		'16分'		=> 16,
		'17分'		=> 17,
		'18分'		=> 18,
		'19分'		=> 19,
		'20分'		=> 20,
		'21分'		=> 21,
		'22分'		=> 22,
		'23分'		=> 23,
		'24分'		=> 24,
		'25分'		=> 25,
		'26分'		=> 26,
		'27分'		=> 27,
		'28分'		=> 28,
		'29分'		=> 29,
		'30分'		=> 30,
		'31分'		=> 31,
		'32分'		=> 32,
		'33分'		=> 33,
		'34分'		=> 34,
		'35分'		=> 35,
		'36分'		=> 36,
		'37分'		=> 37,
		'38分'		=> 38,
		'39分'		=> 39,
		'40分'		=> 40,
		'41分'		=> 41,
		'42分'		=> 42,
		'43分'		=> 43,
		'44分'		=> 44,
		'45分'		=> 45,
		'46分'		=> 46,
		'47分'		=> 47,
		'48分'		=> 48,
		'49分'		=> 49,
		'50分'		=> 50,
		'51分'		=> 51,
		'52分'		=> 52,
		'53分'		=> 53,
		'54分'		=> 54,
		'55分'		=> 55,
		'56分'		=> 56,
		'57分'		=> 57,
		'58分'		=> 58,
		'59分'		=> 59,

		'一分'		=> 1,
		'二分'		=> 2,
		'三分'		=> 3,
		'四分'		=> 4,
		'五分'		=> 5,
		'六分'		=> 6,
		'七分'		=> 7,
		'八分'		=> 8,
		'九分'		=> 9,
		'十分'		=> 10,
		'十一分'		=> 11,
		'十二分'		=> 12,
		'十三分'		=> 13,
		'十四分'		=> 14,
		'十五分'		=> 15,
		'十六分'		=> 16,
		'十七分'		=> 17,
		'十八分'		=> 18,
		'十九分'		=> 19,
		'二十分'		=> 20,
		'二十一分'		=> 21,
		'二十二分'		=> 22,
		'二十三分'		=> 23,
		'二十四分'		=> 24,
		'二十五分'		=> 25,
		'二十六分'		=> 26,
		'二十七分'		=> 27,
		'二十八分'		=> 28,
		'二十九分'		=> 29,
		'三十分'			=> 30,
		'三十一分'		=> 31,
		'三十二分'		=> 32,
		'三十三分'		=> 33,
		'三十四分'		=> 34,
		'三十五分'		=> 35,
		'三十六分'		=> 36,
		'三十七分'		=> 37,
		'三十八分'		=> 38,
		'三十九分'		=> 39,
		'四十分'			=> 40,
		'四十一分'		=> 41,
		'四十二分'		=> 42,
		'四十三分'		=> 43,
		'四十四分'		=> 44,
		'四十五分'		=> 45,
		'四十六分'		=> 46,
		'四十七分'		=> 47,
		'四十八分'		=> 48,
		'四十九分'		=> 49,
		'五十分'			=> 50,
		'五十一分'		=> 51,
		'五十二分'		=> 52,
		'五十三分'		=> 53,
		'五十四分'		=> 54,
		'五十五分'		=> 55,
		'五十六分'		=> 56,
		'五十七分'		=> 57,
		'五十八分'		=> 58,
		'五十九分'		=> 59,
	];
	//整体处理类
	public function str_to_time($str)
	{
		$this->month($str);//月份处理
		$this->number($str);//天数处理
		$this->day($str);//周,具体天处理
		$this->bedeck($str);//修饰词处理
		$this->hour($str);//小时处理
		$this->minute($str);//分数处理

		if(self::$key['modify']=='lower' && self::$key['h']<=12){
			self::$key['h'] = self::$key['h']+12;
		}

		$data = [
			'date' => date(''.self::$key['y'].'-'.self::$key['m'].'-'.self::$key['d'].' '.self::$key['h'].':'.self::$key['i'].''),

			'time' => strtotime(date(''.self::$key['y'].'-'.self::$key['m'].'-'.self::$key['d'].' '.self::$key['h'].':'.self::$key['i'].'')),

			'time_str' => self::$time_str
		];

		return $data;
	}
	//月份如果等于null的话,  默认填入当前年月日
	public function month($str)
	{
		$m = null;

		foreach (self::$month as $k => $v) {
			if(strpos($str,$k)!==false){
				self::$time_str['month']=$k;
				$m=$v;
			}
		}

		if(!is_int($m) && $m!=null){
			$m = date("Y-m",strtotime($m));
		}

		$r = explode('-',$m);

		if($m==null){
			self::$key['y'] = date('Y');
			self::$key['m'] = date('m');
			self::$key['d'] = date('d');
		}else{
			if(count($r)==1){
				self::$key['y'] = date('Y');
				self::$key['m'] = $r[0];
			}else{
				self::$key['y'] = $r[0];
				self::$key['m'] = $r[1];
			}
		}
	}
	//周处理
	public function day($str)
	{
		$d = null;

		foreach (self::$day as $k => $v) {
			if(strpos($str,$k)!==false){
				self::$time_str['day']=$k;
				$d=$v;
			}
		}

		if(!is_int($d) && $d!=null){//今天,明天,后天,大后天
			$d = date("Y-m-d",strtotime($d));
		}elseif($d>7){//下周,或者下下周
			$d = date('Y-m-d', (time() - ((date('w') == 0 ? $d : date('w')) - $d) * 24 * 3600));
		}elseif($d!=null){//本周
			$d = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - $d) * 24 * 3600));
		}

		if($d!=null){
			$r = explode('-',$d);
			self::$key['y'] = $r[0];
			self::$key['m'] = $r[1];
			self::$key['d'] = $r[2];
		}
	}
	//日期处理
	public function number($str)
	{
		$n = date('d');

		$m = self::$key['m'];

		$l = date('L');
		if($m=='4' || $m=='6' || $m=='9' || $m=='11'){
			self::$number = array_slice(self::$number,0,120);
		}elseif($m=='2' && $l){
			self::$number = array_slice(self::$number,0,116);
			//闰年2月29天
		}elseif($m=='2' && !$l){
			self::$number = array_slice(self::$number,0,112);
			//28天
		}

		foreach (self::$number as $k => $v) {
			if(strpos($str,$k)!==false){
				self::$time_str['number']=$k;
				$n=$v;
			}
		}

		self::$key['d']=(string)$n;
	}
	//修饰词处理
	public function bedeck($str)
	{
		$x = '';
		foreach (self::$bedeck as $k => $v) {
			if(strpos($str,$k)!==false){
				self::$time_str['bedeck']=$k;
				$x=$v;
			}
		}

		self::$key['modify'] = $x;
	}
	//小时处理
	public function hour($str)
	{
		if(self::$key['modify']=='upper'){
			$h = '09';
		}elseif(self::$key['modify']=='lower'){
			$h = '13';
		}else{
			$h = '00';
		}
		foreach (self::$hour as $k => $v) {
			if(strpos($str,$k)!==false){
				self::$time_str['hour']=$k;
				$h=$v;
			}
		}
		self::$key['h'] = (string)$h;
	}
	//分钟处理
	public function minute($str)
	{
		$i = '00';
		foreach (self::$minute as $k => $v) {
			if(strpos($str,(string)$k)!==false){
				self::$time_str['minute']=$k;
				$i=$v;
			}
		}

		self::$key['i'] = (string)$i;
	}
}
?>